package com.sanjiahantang.youjiarole.module.join.views.impl;

import android.Manifest;
import android.app.Activity;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.support.annotation.NonNull;
import android.support.v4.content.ContextCompat;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.sanjiahantang.plugin.pickerview.OptionsPickerView;
import com.sanjiahantang.uilibrary.adapter.listview.BaseAdapterHelper;
import com.sanjiahantang.uilibrary.adapter.listview.QuickAdapter;
import com.sanjiahantang.uilibrary.popuwindow.SelectPhotoPopupwindow;
import com.sanjiahantang.uilibrary.utils.BitmapUtils;
import com.sanjiahantang.uilibrary.utils.Utils;
import com.sanjiahantang.uilibrary.views.MyGridView;
import com.sanjiahantang.youjiarole.R;
import com.sanjiahantang.youjiarole.base.BaseMvpActivity;
import com.sanjiahantang.youjiarole.base.BaseView;
import com.sanjiahantang.youjiarole.config.ConfigValue;
import com.sanjiahantang.youjiarole.config.SPConfig;
import com.sanjiahantang.youjiarole.module.common.views.impl.MainActivity;
import com.sanjiahantang.youjiarole.module.join.adapters.ImageAddAdapter;
import com.sanjiahantang.youjiarole.module.join.beans.CertificationRequestBean;
import com.sanjiahantang.youjiarole.module.join.beans.CertificationResponseBean;
import com.sanjiahantang.youjiarole.module.join.beans.ImageBean;
import com.sanjiahantang.youjiarole.module.join.presenters.CertificationPresenter;
import com.sanjiahantang.youjiarole.module.join.views.CertificationView;
import com.sanjiahantang.youjiarole.utils.CheckUtil;
import com.sanjiahantang.youjiarole.utils.IdCardValidator;
import com.sanjiahantang.youjiarole.utils.SPUtils;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;

/**
 *  加盟认证
 * Created by zy on 2017/10/21.
 */

public class CertificationActivity extends BaseMvpActivity<CertificationView,CertificationPresenter> implements CertificationView, View.OnClickListener {


    private View vManager,vSupervision,vShop;
    private MyGridView gvIDCard,gvManage,gvSupervision,gvShop;
    private ImageAddAdapter idAdapter,manageAdapter,supervisionAdapter,shopAdapter;
    private TextView txtCity,txtShopCity,txtPhone;
    private ArrayList<String> listProvince;
    private ArrayList<ArrayList<String>> listCity;
    private ArrayList<ArrayList<ArrayList<String>>> listArea;
    private EditText txtShopAddress,txtName,txtIDCard,txtLinkName,txtLinkTel,txtExperienceName;
    private MyGridView roleGrid;

    private OptionsPickerView pvOptions;
    private int selectorType,picType;
    private List<Integer> roleList;
    private String token;
    private String phone;
    private String rolePassed;
    private QuickAdapter<Integer> roleAdapter;

    private SelectPhotoPopupwindow photoPopupwindow;
    private View rootView;
    private boolean editable = true;

    protected static final int CHOOSE_PICTURE = 0;
    protected static final int TAKE_PICTURE = 1;
    private static final int CROP_SMALL_PICTURE = 2;
    private static final int UPLOAD_PIC = 3;
    private static final int PHOTO_GRAPH = 0xF, SELECT_PICTURE = 3, INTENT_NICKNAME = 5, SAVE_PHOTO = 6;

    private String role;

    @Override
    public void showProgress() {
        super.showProgress($this);
    }

    @Override
    public CertificationPresenter initPresenter() {
        return new CertificationPresenter();
    }

    @Override
    public void initView() {
        setContentView(R.layout.activity_certification);
        rootView = findViewById(R.id.rootView);
        TextView title = (TextView) findViewById(R.id.title);
        title.setText("加盟认证");
        rolePassed = SPUtils.get($this,SPConfig.ROLE_ID,"").toString();
        findViewById(R.id.fl_Left).setOnClickListener(this);
        roleList = new ArrayList<>();
        token = getIntent().getStringExtra(KEY_TOKEN);
        phone = getIntent().getStringExtra(KEY_PHONE);

        if(token == null){
            token = ConfigValue.DATA_KEY;
        }

        if (phone == null){
            phone = SPUtils.get($this,SPConfig.PHONE,"").toString();
        }

        vManager = findViewById(R.id.vManager);
        vSupervision = findViewById(R.id.vSupervision);
        vShop = findViewById(R.id.vShop);
        roleGrid = (MyGridView) findViewById(R.id.gvRole);

        vManager.setVisibility(View.GONE);
        vSupervision.setVisibility(View.GONE);
        vShop.setVisibility(View.GONE);
        gvIDCard = (MyGridView) findViewById(R.id.gvIDCard);
        gvManage = (MyGridView) findViewById(R.id.gvManager);
        gvSupervision = (MyGridView) findViewById(R.id.gvSupervision);
        gvShop = (MyGridView) findViewById(R.id.gvShop);
        idAdapter = new ImageAddAdapter($this,presenter,ImageAddAdapter.TYPE_ID_CARD);
        manageAdapter = new ImageAddAdapter($this,presenter,ImageAddAdapter.TYPE_MANAGER);
        supervisionAdapter = new ImageAddAdapter($this,presenter,ImageAddAdapter.TYPE_SUPERVISION);
        shopAdapter = new ImageAddAdapter($this,presenter,ImageAddAdapter.TYPE_SHOP);
        roleAdapter = new QuickAdapter<Integer>(this,android.R.layout.simple_list_item_1) {
            @Override
            protected void convert(BaseAdapterHelper helper, Integer item) {
                int d5 = Utils.dip2px(context,5);
                LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(d5 * 11,d5 * 5);

                TextView textView = (TextView) helper.getView();
                textView.setBackgroundResource(R.drawable.x_shape_green_border);
                textView.setTextColor(COLOR_PRIMARY);
                textView.setTextSize(10);
                textView.setMaxLines(1);

                params.setMargins(0,0,d5 ,0);
                textView.setPadding(d5,d5 / 2,d5,d5 / 2);
                textView.setGravity(Gravity.CENTER);
                String roleName = "未设置";
                switch (item){
                    case 0:
                        roleName = "设计师";
                        break;
                    case 1:
                        roleName = "项目经理";
                        break;
                    case 2:
                        roleName = "监理";
                        break;
                    case 3:
                        roleName = "体验店";
                        break;
                }
                textView.setText(roleName);
                textView.setLayoutParams(params);
                textView.setOnTouchListener(new View.OnTouchListener() {
                    @Override
                    public boolean onTouch(View v, MotionEvent event) {
                        onRoleClick();
                        return false;
                    }
                });
            }
        };
        roleGrid.setAdapter(roleAdapter);
        findViewById(R.id.vRole).setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                onRoleClick();
                return false;
            }
        });

        idAdapter.setToken(token);
        manageAdapter.setToken(token);
        supervisionAdapter.setToken(token);
        shopAdapter.setToken(token);


        gvIDCard.setAdapter(idAdapter);
        gvManage.setAdapter(manageAdapter);
        gvSupervision.setAdapter(supervisionAdapter);
        gvShop.setAdapter(shopAdapter);

        findViewById(R.id.vCity).setOnClickListener(this);

        txtCity = (TextView) findViewById(R.id.txtCity);
        txtShopCity = (TextView) findViewById(R.id.txtShopCity);
        txtShopAddress = (EditText) findViewById(R.id.txtShopAddress);
        txtName = (EditText) findViewById(R.id.txtName);
        txtPhone = (TextView) findViewById(R.id.txtPhone);
        txtIDCard = (EditText) findViewById(R.id.txtIDCard);
        txtLinkName = (EditText) findViewById(R.id.txtLinkName);
        txtLinkTel = (EditText) findViewById(R.id.txtLinkTel);
        txtExperienceName = (EditText) findViewById(R.id.txtExperienceName);

        txtPhone.setText(phone);

        findViewById(R.id.btnDill).setOnClickListener(this);
        findViewById(R.id.vShopCity).setOnClickListener(this);

        photoPopupwindow = new SelectPhotoPopupwindow($this, this);
        initRolePanel();
    }

    private void initRolePanel() {
        roleList.clear();
        if (!TextUtils.isEmpty(rolePassed)){
            String[] roleArr = rolePassed.split(",");
            for (int i = 0;i < roleArr.length;i++){
                roleList.add(Integer.valueOf(roleArr[i]));
            }

            if (rolePassed.contains("1")){
                vManager.setVisibility(View.VISIBLE);
            }else{
                vManager.setVisibility(View.GONE);
            }

            if (rolePassed.contains("2")){
                vSupervision.setVisibility(View.VISIBLE);
            }else{
                vSupervision.setVisibility(View.GONE);
            }

            if (rolePassed.contains("3")){
                vShop.setVisibility(View.VISIBLE);
            }else{
                vShop.setVisibility(View.GONE);
            }

            if (rolePassed.contains("0") && rolePassed.contains("1") && rolePassed.contains("2") && rolePassed.contains("3")){
                editable = false;
                findViewById(R.id.btnDill).setVisibility(View.GONE);
            }else {
                findViewById(R.id.btnDill).setVisibility(View.VISIBLE);
            }
        }
        roleAdapter.clear();
        roleAdapter.addAll(roleList);

    }

    @Override
    public void initData() {
        presenter.loadPersonCityList($this, new BaseView.OnLoadListener() {
            @Override
            public void onLoad(Object content) {
                Object[] listArray = (Object[]) content;
                listProvince = (ArrayList<String>) listArray[0];
                listCity = (ArrayList<ArrayList<String>>) listArray[1];
                listArea = (ArrayList<ArrayList<ArrayList<String>>>) listArray[2];
                pvOptions = new OptionsPickerView.Builder($this, new OptionsPickerView.OnOptionsSelectListener() {
                    @Override
                    public void onOptionsSelect(int options1, int options2, int options3, View v) {
                        //返回的分别是三个级别的选中位置
                        String tx = listProvince.get(options1) +  " " +
                                listCity.get(options1).get(options2) +  " " +
                                listArea.get(options1).get(options2).get(options3);
                        if (selectorType == 1){
                            txtCity.setText(tx);
                        }

                        if (selectorType == 2){
                            txtShopCity.setText(tx);
                        }

                    }
                })

                        .setTitleText("")
                        .setDividerColor(Color.BLACK)
                        .setTextColorCenter(Color.BLACK) //设置选中项文字颜色
                        .setContentTextSize(20)
                        .build();

                pvOptions.setPicker(listProvince, listCity, listArea);//三级选择器
                presenter.findUserJoin($this, token,phone,new OnLoadListener() {
                    @Override
                    public void onLoad(Object content) {
                        CertificationResponseBean.Data data = (CertificationResponseBean.Data) content;
                        if (data != null){
                            if (data.getRoleInfoList() != null){
                                if (!TextUtils.isEmpty(data.getRoleInfoList().getIdCardPic())){
                                    idAdapter.addAll(getImageList(
                                            data.getRoleInfoList().getIdCardPic().toString().split(",")
                                            ,data.getRoleInfoList().getFileId().split(",")
                                    ));
                                }

                                fillRoleInfoList(data.getRoleInfoList());
                            }

                            if (data.getProjectRaList() != null && !TextUtils.isEmpty(data.getProjectRaList().getWorkQualificationPic())){
                                manageAdapter.addAll(
                                        getImageList(
                                        data.getProjectRaList().getWorkQualificationPic().toString().split(",")
                                        ,data.getProjectRaList().getFileId().split(",")
                                        ));
                            }

                            if (data.getSupervisionRaList() != null && !TextUtils.isEmpty(data.getSupervisionRaList().getSupervisorQualificationPic())){
                                supervisionAdapter.addAll(
                                        getImageList(
                                        data.getSupervisionRaList().getSupervisorQualificationPic().toString().split(","),
                                                data.getSupervisionRaList().getFileId().split(",")
                                        ));
                            }

                            if(data.getExperienceRaList() != null){
                                if (!TextUtils.isEmpty(data.getExperienceRaList().getExperiencePic())){
                                    shopAdapter.addAll(
                                            getImageList(
                                                    data.getExperienceRaList().getExperiencePic().split(","),
                                                    data.getExperienceRaList().getFileId().split(",")
                                            ));
                                }
                                fillExperienceList(data.getExperienceRaList());
                            }
                        }
                    }
                });
            }
        });
    }

    private void fillExperienceList(CertificationResponseBean.ExperienceRaListBean experienceRaList) {
        //体验店所在地
        if (!TextUtils.isEmpty(experienceRaList.getExperienceAddress())){
            txtShopCity.setText(experienceRaList.getExperienceAddress());
        }
        //体验店名称
        if (!TextUtils.isEmpty(experienceRaList.getExperienceName())){
            txtExperienceName.setText(experienceRaList.getExperienceName());
        }

        //体验店详细地址
        if (!TextUtils.isEmpty(experienceRaList.getExperienceAddressDetail())){
            txtShopAddress.setText(experienceRaList.getExperienceAddressDetail());
        }

        //联系人
        if (!TextUtils.isEmpty(experienceRaList.getLinkPersonName())){
            txtLinkName.setText(experienceRaList.getLinkPersonName());
        }

        //联系电话
        if (!TextUtils.isEmpty(experienceRaList.getLinkPersonPhone())){
            txtLinkTel.setText(experienceRaList.getLinkPersonPhone());
        }

        if (!TextUtils.isEmpty(rolePassed) && rolePassed.contains("3")){
            findViewById(R.id.vShopCity).setOnClickListener(null);
            txtExperienceName.setEnabled(false);
            txtShopAddress.setEnabled(false);
            txtLinkName.setEnabled(false);
            txtLinkTel.setEnabled(false);
        }else{
            findViewById(R.id.vShopCity).setOnClickListener(this);
            txtExperienceName.setEnabled(true);
            txtShopAddress.setEnabled(true);
            txtLinkName.setEnabled(true);
            txtLinkTel.setEnabled(true);
        }
    }

    /**
     * 初始化个人信息 内容及是否可编辑
     * @param roleInfoList
     */
    private void fillRoleInfoList(CertificationResponseBean.RoleInfoListBean roleInfoList) {
        if (!TextUtils.isEmpty(roleInfoList.getRealName())){
            txtName.setText(roleInfoList.getRealName());
        }

        if (!TextUtils.isEmpty(roleInfoList.getIdCode())){
            txtIDCard.setText(roleInfoList.getIdCode());
        }

        if (!TextUtils.isEmpty(roleInfoList.getAddress())){
            txtCity.setText(roleInfoList.getAddress());
        }


        if (TextUtils.isEmpty(rolePassed)){
            txtName.setEnabled(true);
            txtIDCard.setEnabled(true);
            findViewById(R.id.vCity).setOnClickListener(this);
        }else{
            txtName.setEnabled(false);
            txtIDCard.setEnabled(false);
            findViewById(R.id.vCity).setOnClickListener(null);
        }
    }

    private Collection<? extends ImageBean> getImageList(String[] pathArr,String[] idArr) {
        List<ImageBean> list = new ArrayList<>();
        for (int i = 0;i < pathArr.length;i++){
            ImageBean bean = new ImageBean();
            bean.setPath(pathArr[i]);
            bean.setId(Integer.parseInt(idArr[i]));
            bean.setType(ImageBean.TYPE_PATH);
            list.add(bean);
        }
        return list;
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.fl_Left:
                finish();
                break;
            case R.id.vCity:
                selectorType = 1;
                pvOptions.show();
                break;
            case R.id.btnDill:


                CertificationRequestBean requestBean = new CertificationRequestBean();
                requestBean.setAddress(txtCity.getText().toString());
                requestBean.setExperienceDetailAddres(txtShopAddress.getText().toString());
                requestBean.setExperienceAddress(txtShopCity.getText().toString());
                requestBean.setLinkPersonName(txtLinkName.getText().toString());
                requestBean.setLinkPersonPhone(txtLinkTel.getText().toString());
                requestBean.setRealName(txtName.getText().toString());
                requestBean.setUserName(txtName.getText().toString());
                requestBean.setIdCode(txtIDCard.getText().toString());
//                requestBean.setLat(ConfigValue.lat == 0 ? DEFAULT_LAT : ConfigValue.lat);
//                requestBean.setLat(ConfigValue.lon == 0 ? DEFAULT_LON : ConfigValue.lon);
                requestBean.setExperienceName(txtExperienceName.getText().toString());
                requestBean.setPhone(phone);
                requestBean.setToken(token);
                ArrayList<Integer> addRoleList = new ArrayList<>();
                if (TextUtils.isEmpty(rolePassed)){
                    addRoleList.addAll(roleList);
                }else{
                    for (int i = 0;i < roleList.size(); i++){
                        if (!rolePassed.contains(String.valueOf(roleList.get(i)))){
                            addRoleList.add(roleList.get(i));
                        }
                    }
                }


                if (TextUtils.isEmpty(rolePassed)){
                    if (TextUtils.isEmpty(txtName.getText())){
                        Utils.showToast($this,"请填写姓名");
                        return;
                    }

                    if (CheckUtil.isName(txtName.getText().toString())){
                        Utils.showToast($this,"请填写中文姓名");
                        return;
                    }

                    if (TextUtils.isEmpty(txtIDCard.getText())){
                        Utils.showToast($this,"请填写身份证号");
                        return;
                    }

                    if (!IdCardValidator.isValidatedAllIdcard(txtIDCard.getText().toString())){
                        Utils.showToast($this,"请填写正确的身份证号");
                        return;
                    }

                    int size = idAdapter.getRealSize();
                    if (size != 2){
                        Utils.showToast($this,"请上传身份证正、反面图片");
                        return;
                    }
                }

                if (addRoleList.size() > 0){
                    requestBean.setRoleList(addRoleList);
                }else{
                    Utils.showToast($this,"请选择您要申请的新角色");
                    return;
                }

                presenter.saveUserJoin($this,requestBean, new OnLoadListener() {
                    @Override
                    public void onLoad(Object content) {
                        SPUtils.put($this,SPConfig.USERNAME,txtName.getText().toString());
                        startActivity(new Intent($this, MainActivity.class).setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP ));
                        Utils.dismissDialog();
                    }
                });
                break;
            case R.id.vShopCity:
                selectorType = 2;
                pvOptions.show();
                break;
            //拍照
            case R.id.btn_take_photo:
                photoPopupwindow.dismiss();
                if (ContextCompat.checkSelfPermission($this, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
                    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                        requestPermissions(new String[]{Manifest.permission.CAMERA}, 1);
                    }
                } else {
                    takePicture();
                }
                break;
            //相册
            case R.id.btn_pick_photo:
                photoPopupwindow.dismiss();
                presenter.cardSelect($this,picType);
                break;
        }
    }



    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == Activity.RESULT_OK){
            switch (requestCode){
                case 0:

                    Uri uri0 = data.getData();
                    presenter.doUpload($this,uri0,UPLOAD_TYPE_ID,token,phone, new OnLoadListener() {
                        @Override
                        public void onLoad(Object content) {
                            String url = (String) content;
                            ImageBean bean0 = new ImageBean();
                            bean0.setPath(url);
                            bean0.setType(ImageBean.TYPE_PATH);
                            idAdapter.add(bean0);
                        }
                    });
                    break;
                case 100:
                    if (data != null){
                        Bundle b = data.getExtras();
                        Bitmap bitmap = (Bitmap) b.get("data");
                        bitmap = BitmapUtils.resizeBitmap(bitmap, 480);
                        presenter.doUpload($this,bitmap,UPLOAD_TYPE_ID,token,phone, new OnLoadListener() {
                            @Override
                            public void onLoad(Object content) {
                                String url = (String) content;
                                ImageBean bean0 = new ImageBean();
                                bean0.setPath(url);
                                bean0.setType(ImageBean.TYPE_PATH);
                                idAdapter.add(bean0);
                            }
                        });
                    }else{
                        Utils.showToast($this,"请从相册选择图片");
                    }

                    break;
                case 1:
                    ImageBean bean1 = new ImageBean();
                    Uri uri1 = data.getData();
                    presenter.doUpload($this,uri1,UPLOAD_TYPE_MANAGER,token,phone, new OnLoadListener() {
                        @Override
                        public void onLoad(Object content) {
                            String url = (String) content;
                            ImageBean bean0 = new ImageBean();
                            bean0.setPath(url);
                            bean0.setType(ImageBean.TYPE_PATH);
                            manageAdapter.add(bean0);
                        }
                    });
                    break;
                case 101:
                    if (data != null){
                        Bundle b = data.getExtras();
                        Bitmap bitmap = (Bitmap) b.get("data");
                        bitmap = BitmapUtils.resizeBitmap(bitmap, 480);
                        presenter.doUpload($this,bitmap,UPLOAD_TYPE_MANAGER,token,phone, new OnLoadListener() {
                            @Override
                            public void onLoad(Object content) {
                                String url = (String) content;
                                ImageBean bean0 = new ImageBean();
                                bean0.setPath(url);
                                bean0.setType(ImageBean.TYPE_PATH);
                                manageAdapter.add(bean0);
                            }
                        });
                    }else{
                        Utils.showToast($this,"请从相册选择图片");
                    }

                    break;
                case 2:
                    ImageBean bean2 = new ImageBean();
                    Uri uri2 = data.getData();
                    presenter.doUpload($this,uri2,UPLOAD_TYPE_SUPERVISION,token,phone, new OnLoadListener() {
                        @Override
                        public void onLoad(Object content) {
                            String url = (String) content;
                            ImageBean bean0 = new ImageBean();
                            bean0.setPath(url);
                            bean0.setType(ImageBean.TYPE_PATH);
                            supervisionAdapter.add(bean0);
                        }
                    });
                    break;
                case 102:
                    if (data != null){
                        Bundle b = data.getExtras();
                        Bitmap bitmap = (Bitmap) b.get("data");
                        bitmap = BitmapUtils.resizeBitmap(bitmap, 480);
                        presenter.doUpload($this,bitmap,UPLOAD_TYPE_SUPERVISION,token,phone, new OnLoadListener() {
                            @Override
                            public void onLoad(Object content) {
                                String url = (String) content;
                                ImageBean bean0 = new ImageBean();
                                bean0.setPath(url);
                                bean0.setType(ImageBean.TYPE_PATH);
                                supervisionAdapter.add(bean0);
                            }
                        });
                    }else{
                        Utils.showToast($this,"请从相册选择图片");
                    }

                    break;
                case 3:
                    ImageBean bean3 = new ImageBean();
                    Uri uri3 = data.getData();
                    presenter.doUpload($this,uri3,UPLOAD_TYPE_SHOP,token,phone, new OnLoadListener() {
                        @Override
                        public void onLoad(Object content) {
                            String url = (String) content;
                            ImageBean bean0 = new ImageBean();
                            bean0.setPath(url);
                            bean0.setType(ImageBean.TYPE_PATH);
                            shopAdapter.add(bean0);
                        }
                    });
                    break;
                case 103:
                    if (data != null){
                        Bundle b = data.getExtras();
                        Bitmap bitmap = (Bitmap) b.get("data");
                        bitmap = BitmapUtils.resizeBitmap(bitmap, 480);
                        presenter.doUpload($this,bitmap,UPLOAD_TYPE_SHOP,token,phone, new OnLoadListener() {
                            @Override
                            public void onLoad(Object content) {
                                String url = (String) content;
                                ImageBean bean0 = new ImageBean();
                                bean0.setPath(url);
                                bean0.setType(ImageBean.TYPE_PATH);
                                shopAdapter.add(bean0);
                            }
                        });
                    }else{
                        Utils.showToast($this,"请从相册选择图片");
                    }

                    break;
                case INTENT_SELECTOR:
                    String role = data.getStringExtra(KEY_PARAM);
                    this.role = role;
                    refreshViews(role);
                    break;
            }
        }
    }

    private void refreshViews(String role) {
        roleAdapter.clear();
        if (role != null){
            roleList.clear();
            if (role.contains("设计师") || rolePassed.contains("0")){
                roleList.add(0);
            }
            if (role.contains("项目经理") || rolePassed.contains("1")){
                roleList.add(1);
                vManager.setVisibility(View.VISIBLE);
            }else {
                vManager.setVisibility(View.GONE);
            }

            if (role.contains("监理") || rolePassed.contains("2")){
                roleList.add(2);
                vSupervision.setVisibility(View.VISIBLE);
            }else {
                vSupervision.setVisibility(View.GONE);
            }

            if (role.contains("体验店") || rolePassed.contains("3")){
                roleList.add(3);
                vShop.setVisibility(View.VISIBLE);
            }else {
                vShop.setVisibility(View.GONE);
            }
            Collections.reverse(roleList);
            roleAdapter.addAll(roleList);
        }
    }

    private void takePicture(){
        String state = Environment.getExternalStorageState();
        if (state.equals(Environment.MEDIA_MOUNTED)) {
            Intent takePhotoIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
            try{
                startActivityForResult(takePhotoIntent, 100 + picType);
            }catch (Exception e){
                e.printStackTrace();
                Utils.showToast($this,"请从相册上传图片");
            }
        } else
            Utils.showToast($this, "未插入SD卡");
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        if (requestCode == 1) {
            if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                takePicture();
            } else {
                Utils.showToast(this, "权限被禁止，相关功能无法使用！");
            }
        }
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);

    }

    public void showAtLocation(int type){
        picType = type;
        photoPopupwindow.showAtLocation(rootView, Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL, 0, 0);
    }

    private void onRoleClick(){
        if (!editable){
            return;
        }
        ArrayList<String> list = new ArrayList<>();
        if (TextUtils.isEmpty(rolePassed)){
            list.add("设计师");
            list.add("项目经理");
            list.add("监理");
            list.add("体验店");
        }else{
            if (!rolePassed.contains("0")){
                list.add("设计师");
            }

            if (!rolePassed.contains("1")){
                list.add("项目经理");
            }

            if (!rolePassed.contains("2")){
                list.add("监理");
            }

            if (!rolePassed.contains("3")){
                list.add("体验店");
            }
        }

        startActivityForResult(
                new Intent($this,RoleTypeSelectorActivity.class)
                        .putExtra(KEY_PARAM,role)
                        .putExtra("list",list)
                ,INTENT_SELECTOR);
    }
}
